package com.glorysoft.hlgdpda.ui.materialandtool.view

import android.os.Bundle
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseActivity
import com.glorysoft.hlgdpda.databinding.ActivityCarrierReplaceBinding
import com.glorysoft.hlgdpda.entity.LotEntity
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.ui.materialandtool.viewmodel.CarrierReplaceViewModel
import kotlinx.android.synthetic.main.activity_carrier_replace.*
import kotlinx.android.synthetic.main.activity_carrier_replace.etCarrier
import kotlinx.android.synthetic.main.activity_carrier_replace.etLot

/**
 * FileName: CarrierReplaceActivity
 * Author: XJH
 * Date: 2021/8/20 15:35
 * Description:
 * History:
 */
class CarrierReplaceActivity : BaseActivity<CarrierReplaceViewModel,ActivityCarrierReplaceBinding>() {
    override fun layoutId(): Int {
        return R.layout.activity_carrier_replace
    }

    override fun initVariableId(): Int {
        return BR.carrierViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("载具更换")
        setRightListener("更换") {
            if (etCarrier.text.toString().trim().isEmpty() || etLot.text.toString().trim().isEmpty() || etNewCarrier.text.toString().trim().isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("请输入载具号或者批次信息",false)
                return@setRightListener
            }
            viewModel.replaceCarrier(etCarrier.text.toString(),etLot.text.toString(),etNewCarrier.text.toString())
        }
    }

    override fun initData() {
        initEdit(etCarrier,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.searchCarrierInfo(s)
            }

        })

        initEdit(etLot,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.searchLotInfo(s)
            }

        })

        viewModel.defUI.callObserve.observe(this) {
            when (it.type) {
                200 -> {
                    val entity = it.item as LotEntity
                    etLot.setText(entity.LOT.LOTID)
                    etCarrier.setText(entity.LOT.DURABLEID)
                }
                100 ->{
                    etCarrier.setText("")
                    etLot.setText("")
                    etNewCarrier.setText("")
                    viewModel.defUI.toastEvent.value = ToastMessageBean("载具更换成功",true)
                }
            }
        }
    }
}